#include <stdio.h>
#include <string.h>
#define N 50


int StrIndex(char *T,char *P) {
	int i=0,j=0,t=strlen(T),p=strlen(P),start=0;
	if(t == 0) return 0;
	while(i < t && j < p) {
		if(T[i] == P[j]) {
			i++;j++;
		} else {
			start++;
			i = start;
			j = 0;
		}
	}
	if (j>=p) return(start);
	else return -1;
}

char* StrRelace(char T[],char P[],char S[],char *str) {
	int i,j,k=0,t=strlen(T),p=strlen(P),s=strlen(S),pos = StrIndex(T,P);
	if(t==0) return NULL;
	for(i=0;i<pos;i++)
		str[i] = T[i];
	str[i] = '\0';
	str = strcat(str,S);
	for(j=0;j<t - pos - p + 1;j++)
		T[j] = T[j + pos+p];
	str = strcat(str, T);
	return str;

} 

int main() {
	char T[N] = "abcdadbcd";
	char P[N]= "bcd";
	char S[N] = "aceb";
	char str[N] = "";
	puts(StrRelace(T,P,S,str));
	return 0;
}
